
/*
 * Created on Feb 5, 2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 * dnquang.
 */
package Utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Thumbnail {
	/**
	 * Thumnail Image 
	 * @param strImgInput: path to file input
	 * @param strImgOutput : path to file output
	 * @param thumbHeight : height of image
	 * @param iQuality : quality when thumnail (100)
	 * return 0:kg co thumbnail vi width < qui dinh
	 *        1: thanh cong
	 *        -1:loi
	 */
	private int th_width;
	private int th_height;
	public  boolean ThumbnailImage(String strImgInput,String strImgOutput,int maxthumbheight,int maxthumbwidth)
	{
		
		int iQuality = 100;
	    try {
	    	Image image = Toolkit.getDefaultToolkit().getImage(strImgInput);
	    
		    MediaTracker mediaTracker = new MediaTracker(new Container());
		    mediaTracker.addImage(image, 0);
	    	mediaTracker.waitForID(0);
	    // determine thumbnail size from WIDTH and HEIGHT
	 	    int imageWidth = image.getWidth(null);
	 	    int imageHeight = image.getHeight(null);
	 	    if(imageWidth<=maxthumbwidth&&imageHeight<=maxthumbheight){
	 	    	this.th_width=imageWidth;
	 	    	this.th_height = imageHeight;
	 	    }
	 	    else{
	 	    	calculateSize(imageHeight, imageWidth, maxthumbheight, maxthumbwidth);
	 	    }
	 	    
	 	if (!new File(strImgInput).exists()){
	 		return false;
	 	}
	 	BufferedImage originalImage =  ImageIO.read(new File(strImgInput));
	 	int type = BufferedImage.TYPE_INT_ARGB;
	 	String ext=strImgInput.substring(strImgInput.lastIndexOf(".")+1);
	 	if (!ext.equalsIgnoreCase("gif")){
	 		try{
	 		type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
	 		}catch (Exception e) {
				type = BufferedImage.TYPE_INT_ARGB;
			}
	 	} else {
	 		ext = "png";
	 	}
	 	
	 	if(ext.equalsIgnoreCase("gif")||ext.equalsIgnoreCase("png")){
		 	BufferedImage thumbImage = new BufferedImage(this.th_width, this.th_height, type);
		 	Graphics2D g = thumbImage.createGraphics();
			g.drawImage(originalImage, 0, 0, this.th_width, this.th_height, null);
			g.dispose();
			ImageIO.write(thumbImage, ext, new File(strImgOutput));
	 	}else{
	 		BufferedImage thumbImage = new BufferedImage(this.th_width, this.th_height, BufferedImage.TYPE_INT_RGB);
		    Graphics2D graphics2D = thumbImage.createGraphics();
		    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		    graphics2D.drawImage(image, 0, 0, this.th_width, this.th_height, null);
//		     save thumbnail image to OUTFILE
		    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(strImgOutput));
		    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
		    iQuality = Math.max(0, Math.min(iQuality, 100));
		    param.setQuality((float)iQuality / 100.0f, false);
		    encoder.setJPEGEncodeParam(param);
		    encoder.encode(thumbImage);
		    out.close(); 
	 	}
		
//	 	BufferedImage thumbImage = new BufferedImage(this.th_width, this.th_height, BufferedImage.TYPE_INT_RGB);
//	    Graphics2D graphics2D = thumbImage.createGraphics();
//	    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//	    graphics2D.drawImage(image, 0, 0, this.th_width, this.th_height, null);
	    // save thumbnail image to OUTFILE
//      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(strImgOutput));
//	    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//	    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
//      iQuality = Math.max(0, Math.min(iQuality, 100));
//	    param.setQuality((float)iQuality / 100.0f, false);
//	    encoder.setJPEGEncodeParam(param);
//	    encoder.encode(thumbImage);
//	    out.close(); 
		return true;
	    }catch (Exception e) {
	    	e.printStackTrace();
	    	return false;
		}
		

	}
	private void calculateSize(int height, int width, int maxsizeheight,int maxsizewidth){
		this.th_height = (int)maxsizewidth*height/width;
		if(this.th_height<=maxsizeheight){this.th_width=maxsizewidth;}
		else{
			this.th_height = maxsizeheight;
			this.th_width = (int)this.th_height*width/height;
		}
	}
	
	public static int getWidth(String input){
		try {
			Image image = Toolkit.getDefaultToolkit().getImage(input);
			MediaTracker mediaTracker = new MediaTracker(new Container());
			mediaTracker.addImage(image, 0);
			mediaTracker.waitForID(0);
			return image.getWidth(null);
		} catch (Exception e) {
			return 0;
		}
	}
	public static int getHeight(String input){
		try {
			Image image = Toolkit.getDefaultToolkit().getImage(input);
			MediaTracker mediaTracker = new MediaTracker(new Container());
			mediaTracker.addImage(image, 0);
			mediaTracker.waitForID(0);
			return image.getHeight(null);
		} catch (Exception e) {
			return 0;
		}
	}
	
	public static void main(String[] args) {
		Thumbnail th = new Thumbnail();
		th.ThumbnailImage("D:\\2.gif", "D:\\2_thumb.gif", 100, 100);
	}
	
}